package com.hnhcyy.b2b.receiver.service;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.alibaba.fastjson.JSONObject;
import com.hnhcyy.b2b.receiver.util.CommonUtil;
import com.hnhcyy.k3cloud.client.HlyDdjylsClient;
import com.hnhcyy.k3cloud.pojo.HlyDdjyls;
import com.hnhcyy.k3cloud.pojo.Zhifjg;
import com.rabbitmq.client.Channel;

/**
 * Title: OrderFlowReceiver  
* Description:  原生支付流水
* @author sheyongjun  
* @date 2019年3月2日
 */
@Component
public class NativePayFlowReceiver {

	private Logger logger = LoggerFactory.getLogger(getClass());
	//金蝶交易流水
	@Autowired
	private HlyDdjylsClient hlyDdjylsClient;

	/**
	 * 支付宝原生支付
	 * @author sheyongjun
	 * @date 2019年3月2日
	 */
	@RabbitListener(queues = "b2b.order.alipay.flow")
	public void alipay(Message message, Channel channel) throws Exception {
		String text = new String(message.getBody(),"utf-8");
		System.out.println("b2b.order.alipay.flow:" + text);
		try {
			
			JSONObject jsonObject = JSONObject.parseObject(text);
			HlyDdjyls hlyDdjyls = new HlyDdjyls();
			
			Zhifjg zhifjgObj = new Zhifjg();
			zhifjgObj.setFNumber("JSFS32_SYS");//JSFS31_SYS：微信  JSFS32_SYS 支付宝
			
			String shanghname = "支付宝商户"; // 商户号
			String shanghddh = jsonObject.getString("out_trade_no");
			String shanghddrq = jsonObject.getString("gmt_payment");
			String ddje = jsonObject.getString("total_amount");// 订单的金额
			String zhifje = jsonObject.getString("buyer_pay_amount");// 用户实际支付的金额
			String dingdzt = jsonObject.getString("trade_status");
			String cplydh = jsonObject.getString("trade_no");
			String beizhu = jsonObject.getString("subject");
			String zhongdh = jsonObject.getString("app_id");
			String jylx = "APP-支付宝";
			
			hlyDdjyls.setFBillNo("").setF_shanghname(shanghname).setF_zhifjg(zhifjgObj)
			.setF_shanghddh(shanghddh).setF_shanghddrq(shanghddrq).setF_ddje(ddje)
			.setF_zhifje(zhifje).setF_cpddh("").setF_jylx(jylx).setF_dingdzt(dingdzt).setF_cpckdh("")
			.setF_cplydh(cplydh).setF_zhongdh(zhongdh).setF_beizhu(beizhu);
			boolean tag = hlyDdjylsClient.add(hlyDdjyls);
			if(tag) {
				channel.basicAck(message.getMessageProperties().getDeliveryTag(), false);
			}else {
				//ack返回false，并重新回到队列
				channel.basicNack(message.getMessageProperties().getDeliveryTag(), false, true);
			}
		} catch (Exception e) {
			e.printStackTrace();
			logger.error("======金蝶交易流水保存出错======");
			//丢弃这条消息（绑定了死信队列，该消息会保存到死信队列）
			channel.basicNack(message.getMessageProperties().getDeliveryTag(), false, false);
		}
	}
	
	/**
	 * 微信原生
	 * @author sheyongjun
	 * @date 2019年3月2日
	 */
//	@RabbitListener(queues = "b2b.order.wechatpay.flow")
	public void wechatpay(Message message, Channel channel) throws Exception {
		String text = new String(message.getBody(),"utf-8");
		System.out.println("b2b.order.wechatpay.flow:" + text);
		
		try {
			JSONObject jsonObject = JSONObject.parseObject(text);
			HlyDdjyls hlyDdjyls = new HlyDdjyls();
			Zhifjg zhifjgObj = new Zhifjg();
			zhifjgObj.setFNumber("JSFS31_SYS");//JSFS31_SYS：微信 / JSFS32_SYS 支付宝
			String shanghname = "微信商户"; // 商户号
			String shanghddh = jsonObject.getString("out_trade_no");
			String shanghddrq = jsonObject.getString("time_end");
			CommonUtil.covertTime(shanghddrq);
			String ddje = jsonObject.getIntValue("total_fee") / 100.0 + "";// 订单的金额
			String zhifje = jsonObject.getIntValue("cash_fee") / 100.0 + "" ;// 用户实际支付的金额
			String dingdzt = jsonObject.getString("result_code");
			String cplydh = jsonObject.getString("transaction_id");
			String beizhu = jsonObject.getString("");
			String zhongdh = jsonObject.getString("mch_id");
			String tradeType = jsonObject.getString("trade_type"); 
			String jylx = "";
			if("NATIVE".equals(tradeType)) { // 微信扫码
				jylx = "PC-微信";
			}else if("JSAPI".equals(tradeType)){ // 微信公众号
				jylx = "公众号-微信";
			}else if("APP".equals(tradeType)){ // 支付宝扫码
				jylx = "APP-微信";
			}
			
			hlyDdjyls.setFBillNo("").setF_shanghname(shanghname).setF_zhifjg(zhifjgObj)
			.setF_shanghddh(shanghddh).setF_shanghddrq(shanghddrq).setF_ddje(ddje)
			.setF_zhifje(zhifje).setF_cpddh("").setF_jylx(jylx).setF_dingdzt(dingdzt).setF_cpckdh("")
			.setF_cplydh(cplydh).setF_zhongdh(zhongdh).setF_beizhu(beizhu);
			
			boolean tag = hlyDdjylsClient.add(hlyDdjyls);
			if(tag) {
				channel.basicAck(message.getMessageProperties().getDeliveryTag(), false);
			}else {
				//ack返回false，并重新回到队列
				channel.basicNack(message.getMessageProperties().getDeliveryTag(), false, true);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	
	/*public static void main(String[] args) throws ParseException {
		String text = "{\"transaction_id\":\"4200000265201903022146940914\",\"nonce_str\":\"f9dae5f252374e00b6cd230375665e35\","
				+ "\"bank_type\":\"CCB_DEBIT\",\"openid\":\"oOU3y0Rh7h4k7WXVbjr8kM3hfvKI\",\"sign\":\"933BCA653E389FA39F9C5BF4258A743A\","
				+ "\"fee_type\":\"CNY\",\"mch_id\":\"1489559842\",\"cash_fee\":\"169948\",\"out_trade_no\":\"19030212151700038312\","
				+ "\"appid\":\"wxff781548f4e076b1\",\"total_fee\":\"169948\",\"trade_type\":\"APP\",\"result_code\":\"SUCCESS\","
				+ "\"time_end\":\"20190302121951\",\"is_subscribe\":\"N\",\"return_code\":\"SUCCESS\"}";
		JSONObject jsonObject = JSONObject.parseObject(text);
		HlyDdjyls hlyDdjyls = new HlyDdjyls();
		
		Zhifjg zhifjgObj = new Zhifjg();
		zhifjgObj.setFNumber("JSFS31_SYS");//JSFS31_SYS：微信 / JSFS32_SYS 支付宝
		String shanghddh = (String) jsonObject.get("out_trade_no");
		String shanghddrq = (String) jsonObject.get("time_end");
		
		CommonUtil.covertTime(shanghddrq);
		
		String ddje = Double.parseDouble((String) jsonObject.get("total_fee"))/100 + "";// 订单的金额
		String zhifje = Double.parseDouble((String) jsonObject.get("cash_fee"))/100 + "" ;// 用户实际支付的金额
		String dingdzt = (String) jsonObject.get("result_code");
		String cplydh = (String) jsonObject.get("transaction_id");
		String beizhu = (String) jsonObject.get("");
		String zhongdh = (String) jsonObject.get("appid");
		String tradeType = (String) jsonObject.get("trade_type"); 
		String jylx = "";
		if("NATIVE".equals(tradeType)) { // 微信扫码
			jylx = "PC-微信";
		}else if("JSAPI".equals(tradeType)){ // 微信公众号
			jylx = "公众号-微信";
		}else if("APP".equals(tradeType)){ // 支付宝扫码
			jylx = "APP-微信";
		}
		
		hlyDdjyls.setFBillNo("").setF_shanghh("").setF_shanghname("").setF_zhifjg(zhifjgObj)
		.setF_shanghddh(shanghddh).setF_shanghddrq(shanghddrq).setF_ddje(ddje)
		.setF_zhifje(zhifje).setF_cpddh("").setF_jylx(jylx).setF_dingdzt(dingdzt).setF_cpckdh("")
		.setF_cplydh(cplydh).setF_zhongdh(zhongdh).setF_beizhu(beizhu);
	}*/

}
